﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Eshouse.Entity;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;
using log4net;
using log4net.Util;

namespace Eshouse.Bll
{
   /// <summary>
   /// 类型操作逻辑，1、如果是ajax类型名称检测，或者是ajax返回单个值等对性能要求高，而且sql语句可以实现多数据库兼容时(如：查询一个值)，就采用ADO.NET方式，其他插入、修改采用NHIBERNATE方式
   /// </summary>
    public class catagoryAccess
    {
        protected ISession Session { get; set; }
        public catagoryAccess()
        {
             NHibernateHelper Nhelper = new NHibernateHelper();
            Session = Nhelper.GetSession();
        }
        /// <summary>
        /// 添加一个类型
        /// </summary>
        /// <param name="catagory"></param>
        /// <returns></returns>
        public bool InsertCatagroy(DicCode catagory)
        {
            bool secces = true;
            
            try
            {
                Session.Save(catagory);
                Session.Flush();
            }catch(Exception ex)
            {
                RecordErr("添加类型", ex);
                secces = false;
            }
            return secces;
        }
        /// <summary>
        /// 检测用户名是否存在,ADO.net方式,实时检测要求性能高一点，不采用NHibernate
        /// </summary>
        /// <param name="catagoryName">类型名称</param>
        /// <returns></returns>
        public static bool VerifyCatagoryName(string catagoryName)
        {
            bool isexist = false;
            string strSql = "select dictID from DicCode where dictName='{0}'";
            DatabaseHelper dbhelper = new DatabaseHelper();
            object o= dbhelper.ExecuteScalar(string.Format(strSql,catagoryName));
            if (o != null)
                isexist = true;
            return isexist;
        }
        /// <summary>
        /// 获取所有类型信息
        /// </summary>
        /// <returns></returns>
        public IList<DicCode> GetCatagoryList()
        {
            IList<DicCode> list;
            try
            {
                list = Session.CreateQuery("from DicCode").List<DicCode>();
            }catch(Exception ex)
            {
                RecordErr("获取所有类型信息失败", ex);
                list= null;
            }
            return list;
        }
        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="userMsg"></param>
        /// <param name="ex"></param>
        private void RecordErr(string userMsg,Exception ex)
        {
            
           
            log4net.ILog log = log4net.LogManager.GetLogger("SMSLogger");
            log.Error(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")+"：" + userMsg, ex);
        }
        /// <summary>
        /// 服务器端 对类型名称的验证
        /// </summary>
        /// <param name="catagoryName">类型名称</param>
        /// <param name="verifyResult">验证结果信息</param>
        /// <returns>是否通过验证</returns>
        public static bool VerifyCatagoryNameOnServer(string catagoryName,ref StringBuilder verifyResult)
        {
            bool verify = true;
            if (catagoryName=="")
            {
                verify = false;
                verifyResult.Append("类型名称不能为空！");
            }
            if (catagoryName.Length<2)
            {
                verify = false;
                verifyResult.Append("类型名称长度不能小于2！");
            }
            if (catagoryAccess.VerifyCatagoryName(catagoryName))
            {
                verify = false;
                verifyResult.Append("该类型名称已经存在！");
            }
            return verify;
        }
    }
}
